package mx.com.asteca.reportes;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mx.com.asteca.comun.dto.EmisionCertificadoDTO;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

/**
 * Servlet implementation class EmiteCertificado
 */

public class EmiteCertificado extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EmiteCertificado() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		List<EmisionCertificadoDTO> listCertificados = (List<EmisionCertificadoDTO>) session.getAttribute("listCertificados");
		String folio = (String) session.getAttribute("paramFolio");
		String leyenda1 = (String) session.getAttribute("leyenda1");
		String leyenda2 = (String) session.getAttribute("leyenda2");
		String logoAsteca = (String) session.getAttribute("logoAsteca");
		InputStream ins2 = this.getServletContext().getResourceAsStream("/WEB-INF/JASPER/certificado"+ ".jrxml");
		try {
			JasperReport report = JasperCompileManager.compileReport(ins2);
			Map<String, Object> param = new HashMap<String, Object>();
			/**
			 * Passing ReportTitle and Author as parameters
			 */
			param.put("paramFolio", folio);
			param.put("paramLeyenda1", leyenda1);
			param.put("paramLeyenda2", leyenda2);
			param.put("imagenLogo", logoAsteca);
			JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(
					listCertificados);
			JasperPrint print = JasperFillManager.fillReport(report, param, ds);
			byte[] file = JasperExportManager.exportReportToPdf(print);
			downloadFile("pdf", file, response);
			System.out.println("DOWNLOAD 2 EXITOSO*************************************");
		} catch (JRException e) {
			System.out.println("ERROR EN EL SEGUNDO INPUT STREAM*******************************");
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	public void downloadFile(String ext, byte[] file,
			HttpServletResponse response) {

		String physicallName = "certificado" + "." + ext;
		String mimeType = "application/vnd.ms-excel";
		if (mimeType != null) {
			response.setContentLength(file.length);
			response.setHeader("Cache-Control",
					"must-revalidate, post-check=0, pre-check=0");
			response.setHeader("Pragma", "public");
			response.setHeader("Content-disposition", "attachment; filename=\""
					+ physicallName + "\"");
			try {
				ServletOutputStream out;
				out = response.getOutputStream();
				out.write(file);
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}

}
